<?php

namespace App\Admin\Controllers;

use App\Models\Flow;
use App\Models\Scene;
use App\Models\SightseeingWay;
use DB;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class SightseeingWayController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('游览方式分析');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(SightseeingWay::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->disableFilter();
            $grid->disableExport();
            $grid->id('ID')->sortable();
            $grid->scene_id('景区')->display(function ($val){
                return Scene::find($val)->title;
            });
            $grid->type('类型');
            $grid->date('日期')->sortable();
            $grid->rate('占比')->display(function ($val){
                return $val.'%';
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(SightseeingWay::class, function (Form $form) {
            $scene_opt = Flow::where('time',0)
                ->groupBy('scene_id')
                ->pluck('scene_id','scene_id')
                ->map(function ($val){
                    return Scene::find($val)->title;
                });
            $form->select('scene_id','景区')->options($scene_opt)->load('date','/api/sightseeingWay/date')->rules('required');
            $form->select('date','日期选择')->rules('required');
            $form->select('type','类型')->options(config('options.sightseeing_way_type'))->rules('required');
            $form->rate('rate','占比')->rules('required');
            //保存前回调
            $form->saved(function (Form $form) {
                $flow = Flow::where('time',0)->where('scene_id',$form->model()->scene_id)->where('date',$form->model()->date)->first();
                $form->model()->num = ceil($flow->num*($form->model()->rate/100));
                if(!$form->model()->save()){
                    $error = new MessageBag([
                        'title'   => '错误',
                        'message' => '添加出错',
                    ]);
                    return back()->with(compact('error'));
                };
            });
        });
    }
}
